package com.kokotchy.tuxcontrol;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentUris;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;

import com.kokotchy.tuxcontrol.provider.MessageList.Messages;

/**
 * Activity for the list of messages already send
 * 
 * $Date: 2009-12-30 15:09:35 +0000 (Wed, 30 Dec 2009) $
 * 
 * @author $Author: kokotchy $
 * @version $Revision: 18 $
 * 
 */
public class MessagesList extends ListActivity {

	/**
	 * TAG used when debugging
	 */
	private static final String TAG = "MessagesList";

	/**
	 * The columns we are interested in from the database
	 */
	private static final String[] PROJECTION = new String[] { Messages._ID, // 0
			Messages.LOCUTOR, // 1
			Messages.MESSAGE, // 2
			Messages.DATE, // 3
	};

	/**
	 * Id for the Clear button in the menu
	 */
	public static final int CLEAR_ID = Menu.FIRST;

	/**
	 * Id for the Resent button in the contextual menu
	 */
	public static final int RESENT_ID = Menu.FIRST + 1;

	/**
	 * Id for the Delete button in the contextual menu
	 */
	public static final int DELETE_ID = Menu.FIRST + 2;

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterView.AdapterContextMenuInfo info;
		try {
			info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
		} catch (ClassCastException e) {
			Log.e(TAG, "bad menuInfo", e);
			return false;
		}
		switch (item.getItemId()) {
		case RESENT_ID:
			Uri messageUri = ContentUris.withAppendedId(getIntent().getData(),
					info.id);
			Intent intent = new Intent();
			intent.setData(messageUri);
			setResult(RESULT_OK, intent);
			finish();
			return true;
		case DELETE_ID:
			Uri uri = ContentUris
					.withAppendedId(getIntent().getData(), info.id);
			Log.v(TAG, "Delete uri " + uri);
			getContentResolver().delete(uri, null, null);
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, RESENT_ID, 0, R.string.menu_resent);
		menu.add(0, DELETE_ID, 0, R.string.menu_delete);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean result = super.onCreateOptionsMenu(menu);
		menu.add(0, CLEAR_ID, 0, R.string.menu_clear);
		return result;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case CLEAR_ID:
			clearMessages();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * Clear all messages stored with a confirmation
	 */
	private void clearMessages() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Are you sure you want to clear all messages?")
				.setCancelable(false).setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								getContentResolver().delete(
										getIntent().getData(), null, null);
							}
						}).setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Messages.CONTENT_URI);
		}

		getListView().setOnCreateContextMenuListener(this);

		Cursor cursor = managedQuery(intent.getData(), PROJECTION, null, null,
				Messages.DEFAULT_SORT_ORDER);

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.message_row, cursor, new String[] { Messages.DATE,
						Messages.LOCUTOR, Messages.MESSAGE }, new int[] {
						R.id.Date, R.id.Locutor, R.id.Message });
		setListAdapter(adapter);
	}

}
